﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Motorway_Simulator.Simulator;
using System.Runtime.Serialization;

namespace Motorway_Simulator.Components
{
    /// <summary>
    /// Vehicle class which sub-vehicles must extends to inherit
    /// methods and properties
    /// </summary>
    [Serializable]
    public class Vehicle : GraphicComponent, IVehicle
    {
        /// <summary>
        /// Holds main properties: max, average, current speed.
        /// </summary>
        protected int maxSpeed, averageSpeed, currentSpeed;

        protected float imageHeight;
        protected float imageWidth;
        /// <summary>
        /// Length of vehicle
        /// </summary>
        private int length;

        /// <summary>
        /// Indicates if it's indicating left
        /// </summary>
        protected bool isIndicatingLeft;

        /// <summary>
        /// Indicates if it's indicating right
        /// </summary>
        private bool isIndicatingRight;

        /// <summary>
        /// Holds maximum acceleration rate
        /// </summary>
        protected int maxAcclerationRate;

        /// <summary>
        /// The vehicles current Point
        /// </summary>
        private Point currentPoint;

        /// <summary>
        /// Holds current road unit it's on
        /// </summary>
        private RoadUnit currentRoadUnit;

        /// <summary>
        /// Holds the next road unit
        /// </summary>
        private RoadUnit nextRoadUnit;

        private int indicationTime;

        public int IndicationTime
        {
            get { return indicationTime; }
            set { indicationTime = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Vehicle"/> class.
        /// </summary>
        /// <param name="startPoint">The start point.</param>
        public Vehicle(Point startPoint, int length, RoadUnit currentRoadUnit)
        {
            this.currentRoadUnit = currentRoadUnit;
            this.length = length;
            currentPoint = currentRoadUnit.Position;
            maxSpeed = 50;
            averageSpeed = 0;
            currentSpeed = 0;
            isIndicatingLeft = false;
            isIndicatingRight = false;
            indicationTime = 0;
        }


        /// <summary>
        /// Gets or sets a value indicating whether this instance is indicating right.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is indicating right; otherwise, <c>false</c>.
        /// </value>
        public bool IsIndicatingRight
        {
            get { return isIndicatingRight; }
            set { isIndicatingRight = value; }
        }


        /// <summary>
        /// Rotates the image clock wise.
        /// </summary>
        /// <param name="images">The images.</param>
        /// <returns></returns>
        protected Bitmap RotateImageClockWise(Bitmap images)
        {
            if (currentRoadUnit.Direction1 == Motorway_Simulator.Components.RoadUnit.Direction.EAST)
            {
                
            }
            else if (currentRoadUnit.Direction1 == Motorway_Simulator.Components.RoadUnit.Direction.SOUTH)
            {
                
                images.RotateFlip(RotateFlipType.Rotate90FlipNone);
                float temp = imageHeight;
                imageHeight = imageWidth;
                imageWidth = temp;
            }
            else if (currentRoadUnit.Direction1 == Motorway_Simulator.Components.RoadUnit.Direction.WEST)
            {
                
                images.RotateFlip(RotateFlipType.Rotate180FlipNone);
              
            }
            else if (currentRoadUnit.Direction1 == Motorway_Simulator.Components.RoadUnit.Direction.NORTH)
            {
                
                images.RotateFlip(RotateFlipType.Rotate270FlipNone);
                float temp = imageHeight;
                imageHeight = imageWidth;
                imageWidth = temp;


            }
            return images;
        }
        /// <summary>
        /// Gets or sets the next road unit.
        /// </summary>
        /// <value>The next road unit.</value>
        public RoadUnit NextRoadUnit
        {
            get { return nextRoadUnit; }
            set { nextRoadUnit = value; }
        }


        /// <summary>
        /// Gets or sets the current road unit.
        /// </summary>
        /// <value>The current road unit.</value>
        public RoadUnit CurrentRoadUnit
        {
            get { return currentRoadUnit; }
            set { currentRoadUnit = value; }
        }

        /// <summary>
        /// Gets or sets the current point.
        /// </summary>
        /// <value>The current point.</value>
        public Point CurrentPoint
        {
            get { return currentPoint; }
            set { currentPoint = value; }
        }

        /// <summary>
        /// Gets the length of the vehicle
        /// </summary>
        public int Length
        {
            get { return length; }

        }

        /// <summary>
        /// Gets the max accleration rate.
        /// </summary>
        /// <value>The max accleration rate.</value>
        public int MaxAcclerationRate
        {
            get { return maxAcclerationRate; }
        }

        /// <summary>
        /// Draws the specified g.
        /// </summary>
        /// <param name="g">The g.</param>
        public override void Draw(Graphics g)
        {

        }

        /// <summary>
        /// Gets or sets the max speed.
        /// </summary>
        /// <value>The max speed.</value>
        public int MaxSpeed
        {
            set { maxSpeed = value; }
            get { return maxSpeed; }
        }

        /// <summary>
        /// Gets or sets the average speed.
        /// </summary>
        /// <value>The average speed.</value>
        public int AverageSpeed
        {
            set { averageSpeed = value; }
            get { return averageSpeed; }
        }

        /// <summary>
        /// Accelerates the specified acceleration rate.
        /// </summary>
        /// <param name="accelerationRate">The acceleration rate.</param>
        /// <returns></returns>
        public bool Accelerate(int accelerationRate)
        {
            if (currentSpeed != maxSpeed)
            {
                currentSpeed += (int)((accelerationRate / 100f) * maxAcclerationRate);
               
                if (currentSpeed > maxSpeed)
                {
                    currentSpeed = maxSpeed;
                }
                return true;
            }
            else return false;
        }

        /// <summary>
        /// Gets or sets the current speed.
        /// </summary>
        /// <value>The current speed.</value>
        public int CurrentSpeed
        {
            get { return currentSpeed; }
            set { currentSpeed = value; }
        }

        /// <summary>
        /// Moves the vehicle 
        /// </summary>
        /// <param name="p">The p.</param>
        /// <returns></returns>
        public bool MoveVehicle(Point p)
        {
            CurrentPoint = p;
         
            return true;
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is indicating.
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance is indicating; otherwise, <c>false</c>.
        /// </value>
        public bool IsIndicatingLeft
        {
            get { return isIndicatingLeft; }
            set { isIndicatingLeft = value; }
        }
    }
}